package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"shop/api/request"
	"shop/initernal/dao"
	"shop/initernal/model"
)

func AddShopComment(c *gin.Context) {
	var req request.AddShopComment
	userId := c.GetUint("userId")
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var data model.Shop
	dao.GetOneByFields(&model.Shop{Id: req.ShopId}, &data)
	if data.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品不存在",
			"data":    nil,
		})
		return
	}
	comment := model.ShopComment{
		ShopId:  req.ShopId,
		UserId:  userId,
		Comment: req.Comment,
	}
	if !dao.Create(&comment) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "新增评论失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "新增评论成功",
		"data":    comment,
	})
}

func QueryShopCommentByShopId(c *gin.Context) {
	var req request.QueryShopCommentByShopId

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var shop model.Shop
	if !dao.GetOneById(req.ShopId, &shop) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品不存在",
			"data":    nil,
		})
		return
	}

	var data []model.ShopComment
	if !dao.GetListByFields(&model.ShopComment{ShopId: req.ShopId}, &data) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "商品列表展示失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "商品列表展示成功",
		"data":    data,
	})

}

func UpdateShopComment(c *gin.Context) {
	var req request.UpdateShopComment

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var shop model.Shop
	if !dao.GetOneById(req.ShopId, &shop) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品不存在",
			"data":    nil,
		})
		return
	}

	var comment model.ShopComment
	dao.GetOneByFields(&model.ShopComment{UserId: c.GetUint("userId")}, &comment)
	if comment.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该评论不存在",
			"data":    nil,
		})
		return
	}

	update := dao.Update(&model.ShopComment{
		Id:      comment.Id,
		ShopId:  shop.Id,
		Comment: req.Comment,
	})
	if !update {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品修改失败",
			"data":    nil,
		})
		return
	}
	var data model.ShopComment

	dao.GetOneByFields(&model.ShopComment{UserId: c.GetUint("userId")}, &data)
	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "商品评论修改成功",
		"data":    data,
	})
}

func DeleteShopComment(c *gin.Context) {
	var req request.DeleteShopComment

	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "获取参数失败",
			"data":    err.Error(),
		})
		return
	}
	var comment model.ShopComment
	dao.GetOneById(req.Id, &comment)
	if comment.Id == 0 {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品评论不存在",
			"data":    nil,
		})
		return
	}

	if comment.UserId != c.GetUint("userId") {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "很抱歉，您没有权限",
			"data":    nil,
		})
		return
	}

	if !dao.Delete(&comment) {
		c.JSON(http.StatusOK, gin.H{
			"code":    10000,
			"message": "该商品删除失败",
			"data":    nil,
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    http.StatusOK,
		"message": "用户评论删除成功",
		"data":    nil,
	})

}
